using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Xml;
using SMS.RoamSMPP.Smpp;
using digit.Core;

namespace SMS.Gateway.Libs
{
    public class ConfigSettings
    {
        // Methods
        private ConfigSettings() { }

        private static string getConfigFilePath()
        {
            return (Assembly.GetExecutingAssembly().Location + ".config");
        }

        public static void LoadConfig()
        {
            ConfigSMPP.RemoteHost = ReadSetting("RemoteHost");
            ConfigSMPP.RemotePort = Convert.ToInt32(ReadSetting("RemotePort"));
            ConfigSMPP.SystemId = ReadSetting("SystemId");
            ConfigSMPP.Password = ReadSetting("Password");
            ConfigSMPP.RemoteHost = ReadSetting("RemoteHost");
            ConfigSMPP.AddressRange = ReadSetting("AddressRange");
            ConfigSMPP.SystemType = ReadSetting("SystemType");
            ConfigSMPP.ConnectionMode = SmppConnectionMode.FromValue(ReadSetting("ConnectionMode"));
            ConfigSMPP.RegisteredDelivery = Convert.ToByte(ReadSetting("RegisteredDelivery"));
            ConfigApp.minThread = Convert.ToInt32(ReadSetting("minThread"));
            ConfigApp.maxThread = Convert.ToInt32(ReadSetting("maxThread"));
            ConfigApp.ConcurrentLimit = Convert.ToInt32(ReadSetting("ConcurrentLimit"));
            ConfigApp.ReLoadMsgOut = Convert.ToInt32(ReadSetting("ReLoadMsgOut"));
            ConfigApp.ConnStr = ReadSetting("ConnStr");
        }

        private static XmlDocument LoadConfigDocument()
        {
            XmlDocument doc1 = null;
            XmlDocument doc2;
            try
            {
                doc1 = new XmlDocument();
                doc1.Load(getConfigFilePath());
                doc2 = doc1;
            }
            catch (FileNotFoundException ex)
            {
                throw new Exception("No configuration file found.", ex);
            }
            return doc2;
        }

        public static string ReadSetting(string key)
        {
            return Util.GetAppSettings(key);
        }

        public static void RemoveSetting(string key)
        {
            XmlDocument document = LoadConfigDocument();
            XmlNode node = document.SelectSingleNode("//appSettings");
            try
            {
                if (node == null)
                    throw new InvalidOperationException("appSettings section not found in config file.");
                
                node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
                document.Save(getConfigFilePath());
            }
            catch (NullReferenceException exception)
            {
                throw new Exception(string.Format("The key {0} does not exist.", key), exception);
            }
        }

        public static void WriteSetting(string key, string value)
        {
            XmlDocument document = LoadConfigDocument();
            XmlNode node = document.SelectSingleNode("//appSettings");
            if (node == null)
                throw new InvalidOperationException("appSettings section not found in config file.");

            try
            {
                XmlElement newChild = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                if (newChild != null)
                    newChild.SetAttribute("value", value);

                else
                {
                    newChild = document.CreateElement("add");
                    newChild.SetAttribute("key", key);
                    newChild.SetAttribute("value", value);
                    node.AppendChild(newChild);
                }
                document.Save(getConfigFilePath());
            }
            catch { }
        }
    }
}

